<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改商铺类型')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-WxShopType-edit" th:object="${wxShopType}">
            <input name="id" th:field="*{id}"  type="hidden">
            <input id="img" th:value="*{imgUrl}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">序号：</label>
                <div class="col-sm-8">
                    <input name="sortNum" th:field="*{sortNum}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">类别名称：</label>
                <div class="col-sm-8">
                    <input name="typeName" th:field="*{typeName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">上传图片：</label>
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="imgFile"  class="file" type="file"  data-theme="fas" accept=".jpg,.img,.png">
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">：</label>
                <div class="col-sm-8">
                    <input name="remarks" th:field="*{remarks}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
        var prefix = ctx + "WxShopType/WxShopType";
        $("#form-WxShopType-edit").validate({
            onkeyup: false,
            rules:{
                typeName:{
                    remote: {
                        url: prefix + "/checkNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "typeName": function () {
                                return $.common.trim($("#typeName").val());
                            },
                            "id": function () {
                                return $.common.trim($("input[name='id']").val());
                            }
                        },
                        dataFilter: function (data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
                sortNum:{
                    remote: {
                        url: prefix + "/checkSortNumUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "sortNum": function () {
                                return $.common.trim($("#sortNum").val());
                            },
                            "id": function () {
                                return $.common.trim($("input[name='id']").val());
                            }
                        },
                        dataFilter: function (data, type) {
                            return $.validate.unique(data);
                        }
                    }
                },
            },
            messages: {
                "typeName":{
                    remote: "类名已存在"
                },
                "sortNum":{
                    remote: "序号重复"
                }
            },
            focusCleanup: true
        });

        var value = document.getElementById('img').value;
        var imgArray = [];
        imgArray.push(ctx+value);
        $(function () {
            //图片回显
            $("#imgFile").fileinput('destroy');
            $("#imgFile").fileinput({
                language: 'zh', //设置语言
                allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀
                uploadAsync: false, //默认异步上传
                showUpload:false,//是否显示上传按钮
                showUpload: false, //是否显示上传按钮
                showRemove: false, //显示移除按钮
                showPreview: true, //是否显示预览
                showCaption: true,//是否显示标题
                browseClass: "btn btn-primary", //按钮样式
                initialPreviewAsData: true,
                initialPreviewFileType: 'image',
                initialPreview: imgArray,
                enctype: 'multipart/form-data',
                validateInitialCount: true
            });
            //删除无用按钮
            $(".file-actions").remove()
        })


        function submitHandler() {
            if ($.validate.form()) {
                var formData = new FormData($("#form-WxShopType-edit")[0]);//收集表单数据
                if ($('#imgFile')[0].files[0] != null) {
                    formData.append('imgFile', $('#imgFile')[0].files[0]);
                }
                $.ajax({
                    url: prefix + "/edit",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function (result) {
                        $.operate.successCallback(result);
                    }
                });

            }
        }
    </script>
</body>
</html>